package com.cookie.meetproject.fragment.chat;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cookie.framework.adapter.CommonAdapter;
import com.cookie.framework.adapter.CommonViewHolder;
import com.cookie.framework.base.BaseFragment;
import com.cookie.framework.bmob.BmobManger;
import com.cookie.framework.bmob.Friend;
import com.cookie.framework.bmob.IMUser;
import com.cookie.framework.utils.CommonUtils;
import com.cookie.meetproject.R;
import com.cookie.meetproject.model.AllFriendModel;
import com.cookie.meetproject.ui.UserInfoActivity;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * 项目名:    MeetProject
 * 包名:      com.cookie.meetproject.fragment.chat
 * 文件名:    AllFriendFragment
 * 创建者:    cookie
 * 创建时间:   2020/1/16 0:34
 * 描述:      所有联系人
 */

public class AllFriendFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {

    private View itemEmptyView;
    private RecyclerView mAllFriendView;
    private SwipeRefreshLayout mAllFriendRefreshLayout;
    private CommonAdapter<AllFriendModel> allFriendModelCommonAdapter;
    private List<AllFriendModel> modelList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_all_record, null);
        initView(view);
        return view;
    }

    private void initView(final View view) {
        itemEmptyView = view.findViewById(R.id.item_empty_view);
        mAllFriendView = view.findViewById(R.id.mAllFriendView);
        mAllFriendRefreshLayout = view.findViewById(R.id.mAllFriendRefreshLayout);
        mAllFriendRefreshLayout.setOnRefreshListener(this);
        mAllFriendView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAllFriendView.addItemDecoration(new DividerItemDecoration(getActivity()
                , DividerItemDecoration.VERTICAL));
        allFriendModelCommonAdapter = new CommonAdapter<>(modelList, new CommonAdapter.OnBindDataListener<AllFriendModel>() {
            @Override
            public void onBindViewHolder(final AllFriendModel model, CommonViewHolder viewHolder, int type, int position) {
                viewHolder.setImageUrl(getActivity(), R.id.iv_photo, model.getUrl());
                viewHolder.setText(R.id.tv_nickname, model.getNickName());
                viewHolder.setText(R.id.tv_desc, model.getDesc());
                viewHolder.setImageResource(R.id.iv_sex, model.isSex() ? R.drawable.img_boy_icon :
                        R.drawable.img_girl_icon);
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UserInfoActivity.startActivity(getActivity(),model.getUserId());
                    }
                });
            }

            @Override
            public int getLayoutId(int type) {
                return R.layout.layout_all_friend_item;
            }
        });
        mAllFriendView.setAdapter(allFriendModelCommonAdapter);
        queryMyFriends();
    }

    //查询所有好友
    private void queryMyFriends() {
        mAllFriendRefreshLayout.setRefreshing(false);
        BmobManger.getInstance().queryMyFriends(new FindListener<Friend>() {
            @Override
            public void done(List<Friend> list, BmobException e) {
                if (e == null) {
                    if (CommonUtils.isEmpty(list)) {
                        itemEmptyView.setVisibility(View.GONE);
                        mAllFriendView.setVisibility(View.VISIBLE);
                        if (modelList.size() > 0) {
                            modelList.clear();
                        }
                        for (int i = 0; i < list.size(); i++) {
                            Friend friend = list.get(i);
                            String objectId = friend.getFriendUser().getObjectId();
                            BmobManger.getInstance().queryObjectIdUser(objectId, new FindListener<IMUser>() {
                                @Override
                                public void done(List<IMUser> list, BmobException e) {
                                    if (e == null) {
                                        if (CommonUtils.isEmpty(list)) {
                                            IMUser imUser = list.get(0);
                                            AllFriendModel model = new AllFriendModel();
                                            model.setUrl(imUser.getPhoto());
                                            model.setNickName(imUser.getNickName());
                                            model.setSex(imUser.isSex());
                                            model.setDesc("签名: " + model.getDesc());
                                            model.setUserId(imUser.getObjectId());
                                            modelList.add(model);
                                            allFriendModelCommonAdapter.notifyDataSetChanged();
                                        }
                                    }
                                }
                            });
                        }
                    } else {
                        itemEmptyView.setVisibility(View.VISIBLE);
                        mAllFriendView.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        if (mAllFriendRefreshLayout.isRefreshing()) {
            queryMyFriends();
        }
    }
}
